package com.jic.finance.impl;

import java.lang.Long;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.finance.common.util.BeanUtil;
import com.jic.finance.entity.PaymentGateway;
import com.jic.finance.mapper.PaymentGatewayMapper;
import com.jic.finance.request.manager.PaymentGatewayRequest;
import com.jic.finance.service.PaymentGatewayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支付网关
 * @author : 
 * @email : 
 * @since : 2020-03-04 10:48:48
 * @version : v1.0.0
 */
@Service
public class PaymentGatewayServiceImpl implements PaymentGatewayService {

    @Autowired
    private PaymentGatewayMapper paymentGatewayMapper;

    @Autowired
    private RedisClientTemplate redisClientTemplate;

    @Override
    public long insert(PaymentGatewayRequest paymentGateway) {
        String userStr = redisClientTemplate.get(paymentGateway.getToken());
        Map sysUser = JSONArray.parseObject(userStr, Map.class);

        PaymentGateway gateway = new PaymentGateway();
        BeanUtils.copyProperties(paymentGateway,gateway);

        gateway.setCreator(Long.valueOf(sysUser.get("id").toString()));
        gateway.setCreatorName(sysUser.get("username").toString());
        gateway.setDeptId(Long.valueOf(sysUser.get("deptId").toString()));
        gateway.setDeleteFlag(0);
        return paymentGatewayMapper.insert(gateway);
    }

    @Override
    public long delete(Long id) {
        return paymentGatewayMapper.delete(id);
    }

    @Override
    public long update(PaymentGatewayRequest paymentGateway) {
        String userStr = redisClientTemplate.get(paymentGateway.getToken());
        Map sysUser = JSONArray.parseObject(userStr, Map.class);

        PaymentGateway gateway = new PaymentGateway();
        BeanUtils.copyProperties(paymentGateway,gateway);

        gateway.setUpdater(Long.valueOf(sysUser.get("id").toString()));
        gateway.setUpdaterName(sysUser.get("username").toString());
        return paymentGatewayMapper.update(gateway);
    }

    @Override
    public PaymentGateway queryByPrimaryKey(Long id){
        return paymentGatewayMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<PaymentGateway> query(PaymentGateway paymentGateway){
        return paymentGatewayMapper.query(paymentGateway);
    }

    @Override
    public PageResult<PaymentGateway> queryPage(PaymentGateway paymentGateway, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<PaymentGateway> list = paymentGatewayMapper.queryPage(startIndex, pageQuery.getLimit(), paymentGateway);
        long count = paymentGatewayMapper.queryPageCount(paymentGateway);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}